using System;
using System.Data;
using System.IO;
using System.Xml;
namespace HKB.Common
{
	public class XmlHelper
	{
		private XmlDocument _xml;
		public XmlHelper()
		{
			this._xml = new XmlDocument();
		}
		public void Load(string path)
		{
			try
			{
				if (this._xml != null)
				{
					this._xml = new XmlDocument();
				}
				this._xml.Load(path);
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
		}
		public DataSet GetDataSet(string path)
		{
			DataSet result;
			try
			{
				DataSet dataSet = new DataSet();
				dataSet.ReadXml(path);
				result = dataSet;
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
			return result;
		}
		public DataSet GetDataSet()
		{
			XmlReader reader = XmlReader.Create(new System.IO.StringReader(this._xml.OuterXml));
			DataSet result;
			try
			{
				DataSet dataSet = new DataSet();
				dataSet.ReadXml(reader);
				result = dataSet;
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message.ToString());
			}
			return result;
		}
		public void SetRootXml(string content)
		{
			this._xml.DocumentElement.InnerXml = content;
		}
		public XmlDocument CreateXml(string version, string encoding, string standalone, string rootname)
		{
			XmlNode documentElement = this._xml.DocumentElement;
			this._xml.AppendChild(this._xml.CreateXmlDeclaration(version, encoding, standalone));
			this._xml.AppendChild(this._xml.CreateElement(rootname));
			return this._xml;
		}
		public XmlDocument GetXml()
		{
			return this._xml;
		}
		public string GetString()
		{
			XmlNode documentElement = this._xml.DocumentElement;
			return documentElement.InnerXml;
		}
		public virtual XmlNodeList GetNodesList(string nodepath)
		{
			return this._xml.SelectNodes(nodepath);
		}
		public virtual XmlNode GetNode(string nodepath)
		{
			return this._xml.SelectSingleNode(nodepath);
		}
		public virtual string GetNodeText(string nodepath)
		{
			XmlNode xmlNode = this._xml.SelectSingleNode(nodepath);
			string result;
			if (xmlNode == null)
			{
				result = null;
			}
			else
			{
				result = xmlNode.InnerText.ToString();
			}
			return result;
		}
		public virtual string GetNodeKey(string nodepath, string key)
		{
			XmlNode xmlNode = this._xml.SelectSingleNode(nodepath);
			string result;
			if (xmlNode == null)
			{
				result = null;
			}
			else
			{
				XmlAttribute xmlAttribute = xmlNode.Attributes[key];
				if (xmlAttribute == null)
				{
					result = null;
				}
				else
				{
					result = xmlAttribute.Value.ToString();
				}
			}
			return result;
		}
		public void SetNodeText(string NodeName, string NodeText)
		{
			XmlNode xmlNode = this._xml.SelectSingleNode(NodeName);
			xmlNode.InnerText = NodeText;
		}
		public bool AddNode(string key, string value)
		{
			XmlNode documentElement = this._xml.DocumentElement;
			bool result;
			if (documentElement == null)
			{
				result = false;
			}
			else
			{
				XmlElement xmlElement = this._xml.CreateElement(key);
				xmlElement.InnerXml = value;
				documentElement.AppendChild(xmlElement);
				result = true;
			}
			return result;
		}
		public bool Save(string path)
		{
			this._xml.Save(path);
			return true;
		}
	}
}
